package com.mall.controller.portal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mall.common.Const;
import com.mall.common.ResponseCode;
import com.mall.common.ServerResponse;
import com.mall.dao.MmallCartMapper;
import com.mall.pojo.MmallUser;
import com.mall.service.ICartService;
import com.mall.util.CookieUtil;
import com.mall.util.JsonUtil;
import com.mall.util.RedisPoolUtil;

@Controller
@RequestMapping("/cart/")
public class CartController {
	
	@Autowired
	private ICartService cartService;
	
	@RequestMapping("/add.do")
	@ResponseBody		//添加商品到购物车中
	public ServerResponse cartAdd(HttpServletRequest request,Integer productId,Integer count){
		
		//检测用户是否登录
		//MmallUser user = (MmallUser)session.getAttribute(Const.CURRENT_USER);
		String loginToken = CookieUtil.readLoginToken(request);

		if(StringUtils.isEmpty(loginToken)){
			return ServerResponse.createByErrorMessage("用户未登录");
		}
		
		String userStr = RedisPoolUtil.get(loginToken);
		MmallUser user = JsonUtil.string2Obj(userStr, MmallUser.class);
		
		if(user == null){
			return ServerResponse.createByErroerCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"未登录,需要登录" );
		}
		
		return cartService.add(productId, user.getId(), count);
		
	}
	
	@RequestMapping("/update.do")
	@ResponseBody
	public ServerResponse cartUpdate(HttpServletRequest request,Integer productId,Integer count){
		
		//检测用户是否登录
		//MmallUser user = (MmallUser)session.getAttribute(Const.CURRENT_USER);
		String loginToken = CookieUtil.readLoginToken(request);

		if(StringUtils.isEmpty(loginToken)){
			return ServerResponse.createByErrorMessage("用户未登录");
		}
		
		String userStr = RedisPoolUtil.get(loginToken);
		MmallUser user = JsonUtil.string2Obj(userStr, MmallUser.class);
		
		if(user == null){
			return ServerResponse.createByErroerCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"未登录,需要登录" );
		}
		
		return cartService.update(user.getId(),productId,count);
		
	}
	
	@RequestMapping("/delete.do")
	@ResponseBody
	public ServerResponse cartDelete(HttpServletRequest request, String productIds){
		
		//检测用户是否登录
		//MmallUser user = (MmallUser)session.getAttribute(Const.CURRENT_USER);
		String loginToken = CookieUtil.readLoginToken(request);

		if(StringUtils.isEmpty(loginToken)){
			return ServerResponse.createByErrorMessage("用户未登录");
		}
		
		String userStr = RedisPoolUtil.get(loginToken);
		MmallUser user = JsonUtil.string2Obj(userStr, MmallUser.class);
		
		if(user == null){
			return ServerResponse.createByErroerCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"未登录,需要登录" );
		}
		
		return cartService.delete(user.getId(),productIds);
		
	}
	
	
	@RequestMapping("/list.do")
	@ResponseBody
	public ServerResponse cartList(HttpServletRequest request){
		
		//检测用户是否登录
		//MmallUser user = (MmallUser)session.getAttribute(Const.CURRENT_USER);
		String loginToken = CookieUtil.readLoginToken(request);

		if(StringUtils.isEmpty(loginToken)){
			return ServerResponse.createByErrorMessage("用户未登录");
		}
		
		String userStr = RedisPoolUtil.get(loginToken);
		MmallUser user = JsonUtil.string2Obj(userStr, MmallUser.class);
		
		if(user == null){
			return ServerResponse.createByErroerCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), "NEED_LOGIN");
		}
		
		return cartService.list(user.getId());
		
	}
	
	@RequestMapping("/selectAll.do")
	@ResponseBody					//全选所有商品
	public ServerResponse selectAll(HttpServletRequest request){
		
		//检测用户是否登录
		//MmallUser user = (MmallUser)session.getAttribute(Const.CURRENT_USER);
		String loginToken = CookieUtil.readLoginToken(request);

		if(StringUtils.isEmpty(loginToken)){
			return ServerResponse.createByErrorMessage("用户未登录");
		}
		
		String userStr = RedisPoolUtil.get(loginToken);
		MmallUser user = JsonUtil.string2Obj(userStr, MmallUser.class);
		
		if(user == null){
			return ServerResponse.createByErroerCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), "NEED_LOGIN");
		}
		
		//checked == 1,代表已勾选
		return cartService.SelectOrUnSelect(user.getId(),null,1);
	}
	
	@RequestMapping("/unSelectAll.do")
	@ResponseBody					//全不选所有商品
	public ServerResponse unSelectAll(HttpServletRequest request){
		
		//检测用户是否登录
		//MmallUser user = (MmallUser)session.getAttribute(Const.CURRENT_USER);
		
		String loginToken = CookieUtil.readLoginToken(request);

		if(StringUtils.isEmpty(loginToken)){
			return ServerResponse.createByErrorMessage("用户未登录");
		}
		
		String userStr = RedisPoolUtil.get(loginToken);
		MmallUser user = JsonUtil.string2Obj(userStr, MmallUser.class);
		
		if(user == null){
			return ServerResponse.createByErroerCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), "NEED_LOGIN");
		}
		
		//checked == 0,代表没有勾选
		return cartService.SelectOrUnSelect(user.getId(),null,0);
		
	}
	
	@RequestMapping("/selectOne.do")
	@ResponseBody					//选中一个商品
	public ServerResponse selectOne(HttpServletRequest request,Integer productId){
		
		//检测用户是否登录
		//MmallUser user = (MmallUser)session.getAttribute(Const.CURRENT_USER);
		
		String loginToken = CookieUtil.readLoginToken(request);

		if(StringUtils.isEmpty(loginToken)){
			return ServerResponse.createByErrorMessage("用户未登录");
		}
		
		String userStr = RedisPoolUtil.get(loginToken);
		MmallUser user = JsonUtil.string2Obj(userStr, MmallUser.class);
		
		if(user == null){
			return ServerResponse.createByErroerCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), "NEED_LOGIN");
		}
		
		//checked == 1,代表已勾选
		return cartService.SelectOrUnSelect(user.getId(),productId,1);
		
	}
	
	@RequestMapping("/unSelectOne.do")
	@ResponseBody					//选中一个商品
	public ServerResponse unSelectOne(HttpServletRequest request,Integer productId){
		
		//检测用户是否登录
		//MmallUser user = (MmallUser)session.getAttribute(Const.CURRENT_USER);
		
		String loginToken = CookieUtil.readLoginToken(request);

		if(StringUtils.isEmpty(loginToken)){
			return ServerResponse.createByErrorMessage("用户未登录");
		}
		
		String userStr = RedisPoolUtil.get(loginToken);
		MmallUser user = JsonUtil.string2Obj(userStr, MmallUser.class);
		
		if(user == null){
			return ServerResponse.createByErroerCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), "NEED_LOGIN");
		}
		
		//checked == 0,代表没有勾选
		return cartService.SelectOrUnSelect(user.getId(),productId,0);
		
	}
	
	@RequestMapping("/getCartProductCount.do")
	@ResponseBody             //查询在购物车里的产品数量
	public ServerResponse getCartProductCount(HttpServletRequest request){
		
		//检测用户是否登录
		//MmallUser user = (MmallUser)session.getAttribute(Const.CURRENT_USER);
		
		String loginToken = CookieUtil.readLoginToken(request);

		if(StringUtils.isEmpty(loginToken)){
			return ServerResponse.createByErrorMessage("用户未登录");
		}
		
		String userStr = RedisPoolUtil.get(loginToken);
		MmallUser user = JsonUtil.string2Obj(userStr, MmallUser.class);
		
		if(user == null){
			return ServerResponse.createBySuccess(0);
		}
		
		return cartService.getCartProductCount(user.getId());
		
	}
	
	
}
